package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.CardUseLog;
import com.tbit.uqbike.object.pojo.vo.AreaVO;
import com.tbit.uqbike.object.pojo.vo.CardUseLogVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.CardUseLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.capital.CardUseLogDao;
import com.tbit.uqbike.webmanager.util.csv.CsvExportUtils;
import com.tbit.uqbike.webmanager.util.csv.entity.ExportHandler;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;

/**
 * 卡券使用记录接口实现类
 *
 * @Author:BUGTian
 * @DATE: 2023/1/6
 */
@Service
public class CardUseLogServiceImpl extends ServiceImpl<CardUseLogDao, CardUseLog> implements CardUseLogService {

    @Autowired
    private CardUseLogDao cardUseLogDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private CsvExportUtils csvExportUtils;

    @Override
    public PageResult<CardUseLogVO> queryPage(Integer page, Integer size, Integer brandId, String buyAccountIds, String useAccountIds, Integer cardType, Date startTime, Date endTime) {
        Assert.notNulls("参数有误", page, size, brandId);
        PageInfo<CardUseLogVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> cardUseLogDao.queryList(brandId, buyAccountIds, useAccountIds, cardType, startTime, endTime));
        List<CardUseLogVO> list = setAccountName(brandId, pageInfo);
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), list);
    }

    private List<CardUseLogVO> setAccountName(Integer brandId, PageInfo<CardUseLogVO> pageInfo) {
        List<CardUseLogVO> list = pageInfo.getList();
        List<AreaVO> areaList = accountDao.getAreaByBrandId(brandId);
        Map<Integer, String> map = new HashMap<>();
        for (AreaVO areaVO : areaList) {
            map.put(areaVO.getAccountId(), areaVO.getName());
        }
        //设置区域名字
        for (CardUseLogVO cardUseLogVO : list) {
            cardUseLogVO.setBuyAccountName(map.get(cardUseLogVO.getBuyAccountId()));
            cardUseLogVO.setUseAccountName(map.get(cardUseLogVO.getUseAccountId()));
        }
        return list;
    }

    @Override
    public String export(Integer brandId, String buyAccountIds, String useAccountIds, Integer cardType, Date startTime, Date endTime) {
        File tempFile = csvExportUtils.getTempFile();
        IntSupplier countFn = () -> cardUseLogDao.queryListCount(brandId, buyAccountIds, useAccountIds, cardType, startTime, endTime);
        Map<Integer, String> areaMap = accountDao.getAreaByBrandId(brandId).stream().collect(Collectors.toMap(AreaVO::getAccountId, AreaVO::getName));
        ExportHandler<CardUseLogVO> exportHandler = selectFn -> {
            final List<CardUseLogVO> logs = cardUseLogDao.queryList(brandId, buyAccountIds, useAccountIds, cardType, selectFn.getStartTime(), selectFn.getEndTime());
            logs.forEach(x -> {
                x.setBuyAccountName(areaMap.get(x.getBuyAccountId()));
                x.setUseAccountName(areaMap.get(x.getUseAccountId()));
            });
            return logs;
        };
        File file = csvExportUtils.exportBigCsvUseDynamicDate(exportHandler, CardUseLogVO.class, countFn,
                startTime, endTime, tempFile, "卡券使用记录");
        String uploadFileName = UUID.randomUUID().toString().replace("-", "");
        return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
    }

}
